package com.beiing.tianshuai.tianshuai.freshnews.model;

import com.beiing.tianshuai.tianshuai.entity.CodeBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

/**
 * Created by Jinni on 2017/9/15 0015.
 */

public class DiscoveryPublishModel implements DiscoveryPublishModelImpl{

    private static final String TAG = "DiscoveryPublishModel";
    private OnRequestResult mListener;

    public DiscoveryPublishModel(OnRequestResult listener) {
        mListener = listener;
    }

    @Override
    public void getPublishResult(String content, String uid, String title, String freshCover) {
        Observable<CodeBean> observable = DataManager.getDiscoveryPublishResult(
                content,
                uid,
                title,
                freshCover
        );
        Observable<CodeBean> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<CodeBean>() {

            @Override
            public void onNext(@NonNull CodeBean bean) {
                if (mListener != null) {
                    mListener.onSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e,0);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    @Override
    public void getUploadImg(File file) {
        //构建body
        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("freshcover", file.getName(), RequestBody.create(MediaType.parse("image/*"), file))
                .build();
        Observable<ResponseBody> observable = DataManager.getUploadImgResult(
                requestBody
        );
        Observable<ResponseBody> subscribeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        subscribeOn.subscribe(new DisposableObserver<ResponseBody>() {

            @Override
            public void onNext(@NonNull ResponseBody bean) {
                if (mListener != null) {
                    mListener.onUploadSuccess(bean);
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                if (mListener != null) {
                    mListener.onError(e,1);
                }
            }

            @Override
            public void onComplete() {
                LogUtils.i("------------------ onCompleted ------------------", TAG);
            }
        });
    }

    public interface OnRequestResult {
        void onSuccess(CodeBean bean);
        void onUploadSuccess(ResponseBody body);
        void onError(Throwable throwable, int i);
    }
}
